<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Object.assign()</title>
</head>
<body>
    <h1>Object.assign()</h1>
    <h2>用于对象的合并，将源对象的所有可枚举属性复制到目标对象</h2>

    <script>
        // 基本用法
        const target = { a: 1 };
        const source1 = { b: 2 };
        const source2 = { c: 3 };
        
        Object.assign(target, source1, source2);
        console.log(target); // {a: 1, b: 2, c: 3}

        // 同名属性的替换
        const target2 = { x: 1 };
        const source3 = { x: 2 };
        Object.assign(target2, source3);
        console.log(target2); // {x: 2}

        // 浅拷贝特性
        const obj = {a: {b: 1}};
        const copy = Object.assign({}, obj);
        obj.a.b = 2;
        console.log(copy.a.b); // 2
    </script>
</body>
</html> 